package com.cb.controller;

import com.alipay.api.AlipayApiException;
import com.cb.pojo.FilmAccess;
import com.cb.pojo.User;
import com.cb.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.text.ParseException;
import java.util.HashMap;

@RestController
@RequestMapping("/user")
@Api(value = "用户的操作", description = "用户的操作")
public class UserController {

    @Autowired
    UserService userService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private RedisTemplate<String, String> redisCacheTemplate;

    /**
     * 用户注册
     * @param user
     * @return
     */
    @PostMapping("/sgin")
    @ResponseBody
    @ApiOperation(value = "用户注册", notes = "用户注册")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "user", value = "user对象")
    })
    public String userInsert(User user,HttpServletRequest request){
        return userService.userInsert(user);
    }

    /**
     * 获取验证码
     * @param json
     */
    @PostMapping("/yanzhengma")
    @ApiOperation(value = "获取验证码", notes = "获取验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "邮箱")
    })
    public void VerificationCode(@RequestBody HashMap json, HttpServletRequest request){
        String phone = String.valueOf(json.get("phone"));
        System.out.println(phone);
        userService.VerificationCode(phone);
    }

    /**
     * 登陆
     * @param json
     * @param request
     * @return
     */
    @PostMapping("/login")
    @ResponseBody
    @ApiOperation(value = "登录", notes = "登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户帐号"),
            @ApiImplicitParam(name = "password", value = "密码"),
    })
    public String login(@RequestBody HashMap json, HttpServletRequest request){
        return userService.login(json,request);
    }

    /**
     * 登录验证
     * @param json
     * @param request
     * @return
     */
    @PostMapping("/yzlogin")
    @ResponseBody
    @ApiOperation(value = "登录验证", notes = "登录验证")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "yzm", value = "用户填写的验证码"),
            @ApiImplicitParam(name = "phone", value = "邮箱"),
    })
    public String yzlogin(@RequestBody HashMap json, HttpServletRequest request){
        return userService.yzlogin(json,request);
    }

    /**
     * 开通会员
     * @param json
     * @param request
     * @return
     * @throws AlipayApiException
     */
    @PostMapping("/joinmembership")
    @ResponseBody
    @ApiOperation(value = "开通会员", notes = "开通会员")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "yyid", value = "影院编号")

    })
    public String joinMemberShip(@RequestBody HashMap json, HttpServletRequest request) throws AlipayApiException, ParseException {
        return userService.joinMemberShip(json,request);
    }

    /**
     * 验证实名制
     * @param request
     * @return
     */
    @PostMapping("/yzzzid")
    @ApiOperation(value = "验证实名制", notes = "验证实名制")
    public String yzZzid(HttpServletRequest request){
        return userService.yzZzid(request);
    }

    
    /**
     * 用户自己验证实名制
     * @param json
     * @param request
     * @return
     */
    @PostMapping("/realnamesystem")
    @ApiOperation(value = "验证实名制", notes = "验证实名制")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "zzid", value = "证件号码"),
            @ApiImplicitParam(name = "name", value = "真实姓名")
    })
    public String realNameSystem(@RequestBody HashMap json, HttpServletRequest request){
        return userService.realNameSystem(json,request);
    }


    /**
     * 判断用户自己是不是会员
     * @param json
     * @param request
     * @return
     */
    @PostMapping("/judgmentVip")
    @ApiOperation(value = "判断用户自己是不是会员", notes = "判断用户自己是不是会员")
    @ApiImplicitParams({

    })
    public String judgmentVip(@RequestBody HashMap json, HttpServletRequest request) throws ParseException {
        return userService.judgmentVip(json,request);
    }

    @PostMapping("/myorderlist")
    @ApiOperation(value = "查看自己的订单，不用传username，我通过session获取", notes = "查看自己的订单，不用传username，我通过session获取")
    @ApiImplicitParams({

    })
    public String myorderlist(@RequestBody HashMap json, HttpServletRequest request) throws ParseException {
        return userService.myorderlist(json,request);
    }

    @PostMapping("/changepassword")
    @ApiOperation(value = "修改密码", notes = "修改密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名"),
            @ApiImplicitParam(name = "jpasswrod", value = "旧密码"),
            @ApiImplicitParam(name = "newpassword", value = "新密码"),
    })
    public String changepassword(@RequestBody HashMap json, HttpServletRequest request) throws ParseException {
        return userService.changepassword(json,request);
    }

    @PostMapping("/Test")
    @ApiOperation(value = "座位测试", notes = "座位测试")
    @ApiImplicitParams({

    })
    public void Test(@RequestBody HashMap json, HttpServletRequest request) throws ParseException {
        String locate = "[[0, 2, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]]";
        int x=6;
        int y=6;
        int[][] str = new int[x][y];
        int num=0;
        int x1=0,y1=-1;
        for (int i=0;i<locate.length();i++){
            if (locate.charAt(i)>='0'&&locate.charAt(i)<='9'){
                num++;
                y1++;
                if (num>x && num%x==1){
                    x1++;
                }
                if (locate.charAt(i)!='0') {
                    str[x1][y1 % x] = Integer.parseInt(String.valueOf(locate.charAt(i)));
                }
            }
        }
        for (int i =0 ;i<str.length;i++){
            for (int j=0;j<str.length;j++){
                System.out.print(str[i][j]+" ");
            }
            System.out.println("");
        }
    }

}
